/**
 * @author 鍚存倍
 * @since 2022/10/12
 * 13:56
 */
package com.windgo.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketKeyword;
import com.windgo.bean.MarketKeywordExample;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.common.SearchDate;
import com.windgo.bean.common.UpdateKeywordInfo;
import com.windgo.mapper.MarketKeywordMapper;
import com.windgo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KeywordServiceImpl implements KeywordService{
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public CommonData<MarketKeyword> list(BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        List<MarketKeyword> list = marketKeywordMapper.select();
        PageInfo<MarketKeyword> listPageInfo = new PageInfo<MarketKeyword>(list);
        int pageNum = listPageInfo.getPageNum();//page
        int pageSize = listPageInfo.getPageSize();//limit
        long total = listPageInfo.getTotal();//total
        int pages = listPageInfo.getPages();//pages
       // List<MarketKeyword> list1 = listPageInfo.getList();
        CommonData<MarketKeyword> data = new CommonData<MarketKeyword>((int)total, pages, pageSize, pageNum, list);
        return  data;

    }

    @Override
    public int delete(Integer id) {
        int i = marketKeywordMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public MarketKeyword update(MarketKeyword marketKeyword) {
        Date date = new Date();
        marketKeyword.setUpdateTime(date);
        int i = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        if(i!=1){
            return null;
        }
        return marketKeyword;
    }

    @Override
    public MarketKeyword insert(MarketKeyword marketKeyword) {
        Date date = new Date();
        marketKeyword.setUpdateTime(date);
        marketKeyword.setAddTime(date);
        int i = marketKeywordMapper.insertSelective(marketKeyword);
        if(i!=1){
            return null;
        }
        Integer id = marketKeyword.getId();
        marketKeyword.setId(id);
        return marketKeyword;
    }

   /* @Override
    public CommonData<MarketKeyword> search(SearchDate searchDate) {
        return null;
    }

    */

    @Override
    public CommonData<MarketKeyword> search(BasePageInfo basePageInfo, String keyword, String url) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if(keyword!=null&&!"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if(url!=null&&!"".equals(url)){
            criteria.andUrlLike("%"+url+"%");
        }
        List<MarketKeyword> list = marketKeywordMapper.selectByExample(marketKeywordExample);
        PageInfo<MarketKeyword> marketKeywordPageInfo = new PageInfo<>(list);
        return CommonData.data(marketKeywordPageInfo);
    }

   /* @Override
    public MarketKeyword insert(MarketKeyword marketKeyword) {
        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());
        int insert = marketKeywordMapper.insertSelective(marketKeyword);
        if(insert!=1){
           return null;
        }
        MarketKeyword marketKeyword1 = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return  marketKeyword1;
    }

    @Override
    public CommonData<MarketKeyword> search(SearchDate searchDate) {
        PageHelper.startPage(searchDate.getPage(),searchDate.getLimit());
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        //进行排序
        marketKeywordExample.setOrderByClause(searchDate.getSort()+" "+searchDate.getOrder());
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        String keyword = searchDate.getKeyword();
        String url = searchDate.getUrl();
        if(StringUtils.isEmpty(keyword)&&StringUtils.isEmpty(url)){
            List<MarketKeyword> list = marketKeywordMapper.select();
        }
        criteria.andKeywordLike("%"+keyword+"%");
        criteria.andUrlLike("%"+url+"%");
        List<MarketKeyword> list = marketKeywordMapper.selectByExample(marketKeywordExample);
        PageInfo<MarketKeyword> marketKeywordPageInfo = new PageInfo<>(list);
        int pageNum = marketKeywordPageInfo.getPageNum();//page
        int pages = marketKeywordPageInfo.getPages();//pages
        int pageSize = marketKeywordPageInfo.getPageSize();//limit
        long total = marketKeywordPageInfo.getTotal();//total
        CommonData<MarketKeyword> data = new CommonData<MarketKeyword>((int)total,pages,pageSize,pageNum,list);
        return data;

    }

    */
}

